package com.caishi.lkx.common.config;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.filters.OpenApiMethodFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(value = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true)
public class SwaggerConfig {

    private final String appVersion = "1.0.0";

    private final OpenApiMethodFilter methodFilter = method -> method.getAnnotation(Operation.class) != null;

    @Bean
    public GroupedOpenApi userApiSwagger() {
        return GroupedOpenApi.builder()
                .group("用户")
                .packagesToScan("com.caishi.lkx.user.api")
                .addOpenApiCustomiser(openApi -> openApi.info(new Info().title("用户 API").version(appVersion)))
                .addOpenApiMethodFilter(methodFilter)
                .build();
    }

    @Bean
    public GroupedOpenApi courseApiSwagger() {
        return GroupedOpenApi.builder()
                .group("学习服务")
                .packagesToScan("com.caishi.lkx.course.api")
                .addOpenApiCustomiser(openApi -> openApi.info(new Info().title("学习服务 API").version(appVersion)))
                .addOpenApiMethodFilter(methodFilter)
                .build();
    }

    @Bean
    public GroupedOpenApi informationApiSwagger() {
        return GroupedOpenApi.builder()
                .group("信息服务")
                .packagesToScan("com.caishi.lkx.Information.api")
                .addOpenApiCustomiser(openApi -> openApi.info(new Info().title("信息服务 API").version(appVersion)))
                .addOpenApiMethodFilter(methodFilter)
                .build();
    }
    @Bean
    public GroupedOpenApi systemApiSwagger() {
        return GroupedOpenApi.builder()
                .group("系统服务")
                .packagesToScan("com.caishi.lkx.system.api")
                .addOpenApiCustomiser(openApi -> openApi.info(new Info().title("系统服务 API").version(appVersion)))
                .addOpenApiMethodFilter(methodFilter)
                .build();
    }
    @Bean
    public GroupedOpenApi examApiSwagger() {
        return GroupedOpenApi.builder()
                .group("考试服务")
                .packagesToScan("com.caishi.lkx.exam.api")
                .addOpenApiCustomiser(openApi -> openApi.info(new Info().title("考试服务 API").version(appVersion)))
                .addOpenApiMethodFilter(methodFilter)
                .build();
    }
    @Bean
    public GroupedOpenApi orderApiSwagger() {
        return GroupedOpenApi.builder()
                .group("订单服务")
                .packagesToScan("com.caishi.lkx.order.api")
                .addOpenApiCustomiser(openApi -> openApi.info(new Info().title("订单服务 API").version(appVersion)))
                .addOpenApiMethodFilter(methodFilter)
                .build();
    }

}
